
const { getUrlParam } = require("./getUrlData");

// 初始化表单时从url上拿到label value 组装成数组给回去.
const getLabelInValue = (labelName, valueName) => {
    let labelStr = decodeURI(getUrlParam(labelName));
    let valueStr = decodeURI(getUrlParam(valueName));
    // console.log("获取到的url上的参数：", labelName + '=' + labelStr, valueName + '=' + valueStr)

    let list = [];
    if (labelStr == null || labelStr == 'null' || labelStr == 'undefined' || labelStr == undefined || valueStr == 'null' || valueStr == null || valueStr == 'undefined' || valueStr == undefined) {
        return [];
    }
    if (labelStr && labelStr.length > 0 && valueStr && valueStr.length > 0) {
        var arr1 = labelStr.split(",");
        var arr2 = valueStr.split(",");
        for (var i = 0; i < arr1.length; i++) {
            list.push({
                label: arr1[i],
                value: arr2[i]
            })
        }

        console.log("连接list", list)
    }
    // console.log("最终数据：", list)


    return list
}

// 传入[{label:"",value:""}]　单独获得里面的label并拼成字符串"label,label形式"
const getLabelOrValue = (key, list, type = 1) => {
    let str = "";
    let arr = [];
    if (list && list.length > 0) {
        arr = list.map(item => {
            return item[key]
        })
    }
    if (arr.length > 0) {
        str = arr.join(",")
    }
    if (type === 1) {
        return str;
    } else {
        return arr;
    }
    // return str;
}

module.exports = {
    getLabelInValue,
    getLabelOrValue

}